﻿using ArtMath.Converter;
using ArtMath.Resource;
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Windows.Forms;

namespace ArtMath.Component.Line.Converter
{
    public class LineSegmentBaseConverter : TypeConverterBase
    {
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string exp = value as string;
            if (exp != null)
            {
                if (context?.Instance != null && !(context.Instance is Array))
                {
                    foreach (PropertyInfo item in context.Instance.GetType().GetProperties())
                    {
                        if (item.Name.Equals(context.PropertyDescriptor.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            LineSegmentBase lsb = item.GetValue(context.Instance, null) as LineSegmentBase;
                            try
                            {
                                lsb.SetFromString(exp);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, Resources.Translate("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            return lsb;
                        }
                    }
                }
                return null;
            }
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            //LineSegmentBase xd = value as LineSegmentBase;
            //if (xd != null)
            //{
            //    string p1 = xd.Point1.ToString(), p2 = xd.Point2.ToString();
            //    if (destinationType == typeof(string))
            //    {
            //        return p1 + ", " + p2;
            //    }
            //    else if (destinationType == typeof(InstanceDescriptor))
            //    {
            //        string[] str1 = Regex.Split(p1, ", "), str2 = Regex.Split(p2, ", ");
            //        return new InstanceDescriptor(getSpecificType(xd).GetConstructor(
            //            new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) }),
            //            new string[] { str1[0], str1[1], str2[0], str2[1] });
            //    }
            //}
            if (value is LineSegmentBase)
            {
                string str = value.ToString();
                if (destinationType == typeof(string))
                {
                    return str;
                }
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, System.Attribute[] attributes)
        {
            if (value is LineSegmentBase)
            {
                return TypeDescriptor.GetProperties(value.GetType(), attributes);
            }
            return base.GetProperties(context, value, attributes);
        }
    }
}
